package org.cheniy.domain.award.service.distribute.impl;

import lombok.extern.slf4j.Slf4j;
import org.cheniy.domain.award.model.aggregate.GiveOutPrizesAggregate;
import org.cheniy.domain.award.model.entity.DistributeAwardEntity;
import org.cheniy.domain.award.model.entity.UserAwardRecordEntity;
import org.cheniy.domain.award.model.entity.UserCreditAwardEntity;
import org.cheniy.domain.award.model.valobj.AwardStateVO;
import org.cheniy.domain.award.repository.IAwardRepository;
import org.cheniy.domain.award.service.distribute.IDistributeAward;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author cheniy
 * @description 用户非积分-物质奖品发放
 * @create 2025/3/27 22:20
 */
@Slf4j
@Service("user_substance_award")
public class UserSubstanceAward implements IDistributeAward {

    @Resource
    private IAwardRepository iAwardRepository;
    @Override
    public void giveOutPrizes(DistributeAwardEntity distributeAwardEntity) {
        log.info("实质商品转固定积分 userId:{} orderId:{} awardId:{}",distributeAwardEntity.getAwardId(),distributeAwardEntity.getOrderId(),distributeAwardEntity.getAwardId());
        // TODO 暂时没有实质奖品，统一分发10积分
        BigDecimal creditAmount= BigDecimal.valueOf(10);

        // 构建聚合对象
        UserAwardRecordEntity userAwardRecordEntity = GiveOutPrizesAggregate.buildDistributeUserAwardRecordEntity(
                distributeAwardEntity.getUserId(),
                distributeAwardEntity.getOrderId(),
                distributeAwardEntity.getAwardId(),
                AwardStateVO.complete
        );

        UserCreditAwardEntity userCreditAwardEntity = GiveOutPrizesAggregate.buildUserCreditAwardEntity(distributeAwardEntity.getUserId(), creditAmount);

        GiveOutPrizesAggregate giveOutPrizesAggregate = new GiveOutPrizesAggregate();
        giveOutPrizesAggregate.setUserId(distributeAwardEntity.getUserId());
        giveOutPrizesAggregate.setUserAwardRecordEntity(userAwardRecordEntity);
        giveOutPrizesAggregate.setUserCreditAwardEntity(userCreditAwardEntity);

        // 存储发奖对象
        // TODO 大营销： 需要重新写一个方法，实现实体奖品方法（添加聚合对象，用户地址，实体奖品，中奖记录，用户ID）
        iAwardRepository.saveGiveOutPrizesAggregate(giveOutPrizesAggregate);
    }
}
